<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <title>聊天室</title>
  <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    d {
      color: #330118;
    }
    body {
      background-color: #21242b;
      margin: 0;
      padding-bottom: 3rem;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
      padding-top: 0.5rem;
    }

    #form {
      background: rgba(0, 0, 0, 0.15);
      padding: 0.25rem;
      position: fixed;
      bottom: 0;
      left: 0;
      right: 0;
      display: flex;
      height: 3rem;
      box-sizing: border-box;
      backdrop-filter: blur(10px);
    }

    #input {
      border: none;
      padding: 0 1rem;
      flex-grow: 1;
      border-radius: 2rem;
      margin: 0.25rem;
    }

    #input:focus {
      outline: none;
    }

    #form>button {
      background: #333;
      border: none;
      padding: 0 1rem;
      margin: 0.25rem;
      border-radius: 3px;
      outline: none;
      color: #fff;
    }
    #messages {
      list-style-type: none;
      margin: 0;
      padding: 0;
    }
    #messages li {
      display: flex;
    }
    #messages li img {
      box-shadow: 0 0 8px rgb(152, 152, 152);
      display: block;
      width: 45px;
      height: 45px;
      border-radius: 50%;
      margin-right: 15px;
    }
    #messages>li {
      padding: 0.5rem 1rem;
    }
    .pfp-box img {
      width: 50px;
      height: 50px;
    }
    .username {
      margin-bottom: 5px;
      color: #7ab5dd;
      font-weight: bold;
    }
    .msg {
      color: #eef1f3;
    }
    .flex-msg {
      display: flex;
      flex-direction: column;
      width: 100%;
    }
    .date {
      margin-top: 5px;
      font-size: 12px;
      color: #8BA2AD;
    }
  </style>
</head>

<body>
  <ul id="messages">
  </ul>
  <form id="form" action="">
    <input id="input" autocomplete="off" /><button>发送</button>
    <button id="toggle-btn">断开连接</button>
  </form>
  <script src="https://cdn.socket.io/4.7.5/socket.io.min.js"></script>
  <script>
    const imgSrc = Math.floor(Math.random()*18)

    let username;
    username = prompt("请输入用户名")

    const toggleButton = document.getElementById('toggle-btn');
    toggleButton.addEventListener('click', (e) => {
      e.preventDefault();
      if (socket.connected) {
        toggleButton.innerText = 'Connect';
        socket.disconnect();
      } else {
        toggleButton.innerText = 'Disconnect';
        socket.connect();
      }
    });
    const socket = io();

    const form = document.getElementById('form');
    const input = document.getElementById('input');
    const messages = document.getElementById('messages');

    form.addEventListener('submit', (e) => {
      e.preventDefault();
      if(username !== null && username !== undefined) {
        if (username.trim() == '') {
          username = prompt("请先输入用户名！")
        } else {
          if (input.value) {
            socket.emit('chat message', {msg: input.value, username: username.trim()});
            input.value = '';
          }
        }
      } else {
        username = prompt("请先输入用户名！")
      }
    });

    socket.on('chat message', (data) => {
      const now = new Date();
      // const year = now.getFullYear();
      // const month = now.getMonth() + 1;
      // const day = now.getDate();
      const hour = now.getHours();
      const minute = now.getMinutes();
      const sec = now.getSeconds()
      
      $(messages).append(`
        <li>
            <img src="pfp/${imgSrc}.jpg" alt="">
            <div class="flex-msg">
              <span class="username">${data.username}</span>
              <p class="msg">${data.msg}&ensp;&ensp;&ensp;<span class="date">${hour+':'+minute+':'+sec}</span></p>
            </div>
       </li>
      `);
      window.scrollTo(0, document.body.scrollHeight);
    });
  </script>
</body>

</html>